## --------------------------------------------------------------------------

def PrintFrame(frame):
    # print out frame
    # determine if frame contains zeros -> shift_frame

    shift_frame = 0
    print "\nmin",
    for i in range(0,18):
        print "zr#%02i " %(i+1),
    print "press  temp0  temp1   HV1    HV2    HV3  deltaT   hour"

    for i in range(0,60):
        linesum = 0
        print "\n %02i" %(i), 
        for j in range(0,26):
            print "%5i " %(frame[i][j]),
            linesum = linesum + frame[i][j]
        #c=0.0549254*(256*256*frame[i][25]+frame[i][24])
        #print c,
        #print "%02i:" %(c div 3600) # time in seconds
        if (linesum == 0):
            ## empty line found -> all data needs to be shifted to the end of the frame
            ## check if hour in last line = current_hour+1 ?
            print "NO DATA",
            if not(shift_frame):
                shift_frame = i

    print "\n"

    return(shift_frame)

## --------------------------------------------------------------------------

def frame2BIN(frame):
    # convert frame to BIN chunk

    chunk = ''
    for i in range(0,60):
        for j in range(0,26):
            word = frame[i][j]
            hb   = int(word / 256)
            lb   = (word - 256*hb)
            word2 = lb+256*hb
            #print "%5i %3i %3i %5i" %(word, lb, hb, word2)

            chunk += chr(lb)+chr(hb)

    return(chunk)

## --------------------------------------------------------------------------

def BIN2frame(chunk):
    # convert BIN data to frame

    # create a 60*26 array filled with zero
    frame = []
    for i in range (0,60):
        frame.append([0]*26)

    for i in range(0,60):
        for j in range(0,26):
            word = ord(chunk[26*2*i+2*j+0])+256*ord(chunk[26*2*i+2*j+1])
            frame[i][j] = word

    return(frame)

## --------------------------------------------------------------------------

def ShiftFrame(frame,shift_frame):
    # missing data leads to zeros at the end of a BIN file
    # valid data is recorded at the end of the hour
    # non-zero data has to be shifted to the end of the frame
    
    for i in range(0,60):
        line = 60-i-1
        print line,
        if line >= (60-shift_frame):
#            print "copy data from line %i to line %i" %(shift_frame-1-i, line)
            for j in range(0,26):
                frame[line][j] = frame[shift_frame-1-i][j]
        else:
#            print "set to zero"
            for j in range(0,26):
                frame[line][j] = 0

    return(frame)
            
## --------------------------------------------------------------------------
